﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Payroll.BusinessLogic;
using Payroll.Common.Utils;
using Payroll.Common.Utils.Html;
using PayrollHR.Web;
using Payroll.BusinessLogic.CustomerManager;
using Payroll.BusinessLogic.Directory;
using Payroll.BusinessLogic.Utils;
using System.IO;
using OfficeOpenXml;

namespace PayrollHR.Administration.Modules
{
    public partial class CustomersControl : BasePayrollAdministrationUserControl
    {
        private const string m_filetemplate = "CustomerList.xlsx";
        private const int m_pageSize = 50;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                BindGrid();
            }
        }
        protected void BindGrid()
        {
            var customers = GetCustomers();
            gvCustomers.DataSource = customers;
            gvCustomers.DataBind();
        }
        protected List<Customer> GetCustomers()
        {
            CustomerSearchResult customerColection = ctrCustomerSearch.GetCustomerSearch();
            List<Customer> customer = CustomerService.GetAllCustomer(customerColection);
            return customer;
        }
        protected void gvCustomers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvCustomers.PageIndex = e.NewPageIndex;
            BindGrid();
        }
        #region btn
        protected void SearchButton_Click(object sender, EventArgs e)
        {
            this.BindGrid();
        }     
        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                try
                {
                    List<int> customerColection = new List<int>();
                    foreach (GridViewRow row in gvCustomers.Rows)
                    {
                        CheckBox cbCustomerInfo = row.FindControl("cbCustomerInfo") as CheckBox;
                        HiddenField hfCustomerID = row.FindControl("hfCustomerID") as HiddenField;
                        int CustomerID = int.Parse(hfCustomerID.Value);
                        if (cbCustomerInfo.Checked)
                        {
                            customerColection.Add(CustomerID);
                        }
                    }
                    if (customerColection.Count > 0)
                    {
                        this.CustomerService.Delete(customerColection);
                        this.BindGrid();
                    }
                }
                catch (Exception exc)
                {
                    ProcessException(exc);
                }
            }
        }
        #endregion

        protected void btnExport_Click(object sender, EventArgs e)
        {
            string fileName = string.Format("CustomerList{0}", DateTime.Now.ToString("dd-MM-yyyy_HH.mm") + ".xlsx");

           
            FileInfo newFile = new FileInfo(Server.MapPath(@"..\Administration\FileExport\" + fileName));
            FileInfo template = new FileInfo(Server.MapPath(@"..\Files\Template\Customer.xlsx"));
            var listcust = GetCustomers();
            int maxResult = listcust.Count;
            writeSheets(newFile, template, maxResult, listcust);
            //  ExcelHelper.ExportCustomerToExcel(customer, ref filePath, fileTeamplate);
            hlexport.Visible = true;
            hlexport.Text = "Save As File Export";
            hlexport.NavigateUrl = "../FileExport/" + fileName;    
        }
        private void writeSheets(FileInfo newFile, FileInfo template, int maxRow, List<Customer> cust)
        {
            int index = 0;
            int rowIndexCurrentRecord = 2;
            using (var xlPackage = new ExcelPackage(newFile, template))
            {
                ExcelWorksheet worksheet = xlPackage.Workbook.Worksheets[1];

                while (index < maxRow)
                {
                    worksheet.Cells["A" + rowIndexCurrentRecord].Value = index + 1;// stt.ToString();
                    worksheet.Cells["B" + rowIndexCurrentRecord].Value = cust[index].CustName;
                    worksheet.Cells["C" + rowIndexCurrentRecord].Value = cust[index].CustTypeName;
                    worksheet.Cells["D" + rowIndexCurrentRecord].Value = cust[index].CustAddress;
                    worksheet.Cells["E" + rowIndexCurrentRecord].Value = cust[index].CustMobile;
                    worksheet.Cells["F" + rowIndexCurrentRecord].Value = cust[index].CustPhoneNumber;
                    worksheet.Cells["G" + rowIndexCurrentRecord].Value = cust[index].CustEmail;
                    worksheet.Cells["H" + rowIndexCurrentRecord].Value = cust[index].CustContact;
                    rowIndexCurrentRecord++;
                    index++;
                }
                xlPackage.Save();
            }
        }
    }
}